﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace EmailStorm
{
    public partial class Form1 : Form, IMessageFilter  
    {
        public Form1()
        {
            

            InitializeComponent();
            Application.AddMessageFilter(this);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            LoadTreeView(); //加载任务列表

            BindTask(0);//加载任务

            treeView1.Nodes[0].ExpandAll();
            treeView1.SelectedNode = treeView1.Nodes[0].Nodes[0];
        }

        private void LoadTreeView() {    
            Data.Task obj = new EmailStorm.Data.Task();
            DataSet ds=obj.GetTaskList();
            if (ds.Tables[0].Rows.Count == 0)
            {
                TreeNode node = new TreeNode();
                node.Text = "new search";
                treeView1.Nodes[0].Nodes.Add(node);
            }
            else {
                foreach (DataRow row in ds.Tables[0].Rows) {
                    //ListViewItem item = new ListViewItem(row["task_name"].ToString(),0);
                    //item.Group = taskList.Groups[0];
                    //item.Tag = row["task_id"].ToString();
                    TreeNode node = new TreeNode();
                    node.Text = row["keyword"].ToString();
                    node.Tag = row["task_id"];
                    int status =Convert.ToInt32(row["status"]);
                    treeView1.Nodes[status].Nodes.Add(node);
                }

                
            }
        }
        private void BindTask(int taskId) {
            WorkingTask=SwitchTask(taskId);//切换当前工作
            WorkingTask.TaskId = taskId;


            Data.Task obj = new EmailStorm.Data.Task();

            IDataReader reader = obj.GetTaskById(taskId);
            if (reader.Read()) {
                tb_keyword.Text=reader["keyword"].ToString();
                WorkingTask.PageIndex = Convert.ToInt32(reader["page_index"]);//当前搜索的页码
                WorkingTask.PageCount = Convert.ToInt32(reader["page_count"]);//页数
                
            }
            reader.Close();
            dataGridView1.AutoGenerateColumns = false;
            Data.Email email = new EmailStorm.Data.Email();
            DataSet ds=email.GetEmailList(taskId);

            WorkingTask.EmailList = ds.Tables[0];

            dataGridView1.DataSource = WorkingTask.EmailList;

            
        }







        private void taskList_MouseUp(object sender, MouseEventArgs e)
        {

            //ListViewItem item = this.taskList.GetItemAt(e.X, e.Y);
            //if (item == null)
            //{
            //    this.taskList.FocusedItem.Selected = true;
            //}

         
        }


        #region IMessageFilter 成员

        public bool PreFilterMessage(ref Message m)
        {
            //throw new NotImplementedException();
            return false;
           
        }

        #endregion

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //Data.Task obj = new EmailStorm.Data.Task();
            //int taskId = Convert.ToInt32(taskList.SelectedItems[0].Tag);
            //if (taskId == -1)
            //{
            //    taskId=obj.AddTask(taskList.SelectedItems[0].Text, tb_keyword.Text);
            //    taskList.SelectedItems[0].Tag = taskId;
                
            //}
            //else {
            //    obj.EditTask(Convert.ToInt32(taskList.SelectedItems[0].Tag), taskList.SelectedItems[0].Text, tb_keyword.Text);
            //    //obj.UpdateTaskInfo(taskId, 1, 10);
            //}

            //MessageBox.Show("success saved.");
           
            
        }

        private void tb_keyword_Click(object sender, EventArgs e)
        {
            if (tb_keyword.Text == "please input your keyword") {
                tb_keyword.Text ="";
                tb_keyword.ForeColor = Color.Black;
            }
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private App.SearchTask WorkingTask;
        private void btn_search_Click(object sender, EventArgs e)
        {

            WorkingTask.StartSearch();
        }

        public Hashtable TaskList = new Hashtable();

        private App.SearchTask SwitchTask(int taskId) {
            App.SearchTask task=null;
            if(TaskList.Contains(taskId.ToString())){
                task = (App.SearchTask)TaskList[taskId.ToString()];   
            }else{
                task = new App.SearchTask();
            }
            task.TaskId = taskId;
            task.StatusLabel = this.toolStripStatusLabel1;
            task.GridView = this.dataGridView1;
            WorkingTask = task;

            return task;
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //WorkingTask.EndTask();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (WorkingTask != null)
            {
                WorkingTask.EndTask();
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                int taskId = Convert.ToInt32(e.Node.Tag);
                BindTask(taskId);
            }
        }






    }
}
